package com.hzjiteng.newsbymvp.news.widget;

import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.hzjiteng.newsbymvp.R;
import com.hzjiteng.newsbymvp.bean.NewsBean;
import com.hzjiteng.newsbymvp.news.presenter.NewsDetailPresener;
import com.hzjiteng.newsbymvp.news.presenter.NewsDetailPresenerImpl;
import com.hzjiteng.newsbymvp.news.views.NewsDetailView;
import com.hzjiteng.newsbymvp.swipeback.SwipeBackLayout;
import com.hzjiteng.newsbymvp.swipeback.app.SwipeBackActivity;
import com.hzjiteng.newsbymvp.utils.ToolsUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.sufficientlysecure.htmltextview.HtmlTextView;

/**
 * 新闻详情页
 *
 * @author Administrator
 * @version 1.0
 * @Email:lijiayan_mail@163.com
 * @created_time 2016/07/30 9:27
 */
public class NewsDetilsActivity extends SwipeBackActivity implements NewsDetailView {

    private static final String TAG = "NewsDetilsActivity";
    private CollapsingToolbarLayout collapsingTollbarLayout;
    private ImageView iv_image;
    private Toolbar toolbar;
    private ProgressBar progrss;
    private HtmlTextView tv_html;

    private SwipeBackLayout swipeBackLayout;

    private NewsBean bean;

    private NewsDetailPresener newsDetailPresener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_detail_activity);
        initUI();
        aboutToolBar();
        aboutSwipeBackLayout();
        setupData();
        newsDetailPresener.loadNewsDetail(bean.getDocid());
    }

    private void setupData() {
        bean = (NewsBean) getIntent().getSerializableExtra("news");
        collapsingTollbarLayout.setTitle(bean.getTitle());
        ImageLoader.getInstance().displayImage(bean.getImgsrc(),iv_image);
        newsDetailPresener = new NewsDetailPresenerImpl(this);
    }

    private void aboutSwipeBackLayout() {
        swipeBackLayout = getSwipeBackLayout();
        swipeBackLayout.setEdgeSize(ToolsUtil.getWidthInPx(this));
        swipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
    }

    private void aboutToolBar() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initUI() {
        collapsingTollbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingTollbarLayout);
        iv_image = (ImageView) findViewById(R.id.iv_iamge);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        progrss = (ProgressBar) findViewById(R.id.progrss);
        tv_html = (HtmlTextView) findViewById(R.id.tv_html);
    }

    @Override
    public void showNewsDetailContent(String newsDetailContent) {
        Log.e(TAG,"返回的数据是:"+newsDetailContent);
        tv_html.setHtmlFromString(newsDetailContent, new HtmlTextView.LocalImageGetter());
    }

    @Override
    public void showProgress() {
        progrss.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progrss.setVisibility(View.GONE);
    }
}
